package com.yxb.service.server.interfimpl;

import com.yxb.service.common.domain.UserDevice;
import com.yxb.service.common.interf.UserDeviceService;
import com.yxb.service.common.option.UserDeviceOption;
import com.yxb.service.server.manager.UserDeviceManager;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userDeviceService")
public class UserDeviceServiceImpl   implements UserDeviceService {

    private static final Logger  LOGGER   = LoggerFactory.getLogger(UserDeviceServiceImpl.class);

    @Resource
    private UserDeviceManager  userDeviceManager;

    @Override
    public ImmutableTriple<Boolean, String, UserDevice> addUserDevice(UserDevice userDevice) {

        UserDeviceOption  userDeviceOption  = new UserDeviceOption();
        userDeviceOption.setDeviceId(userDevice.getDeviceId());
        userDeviceOption.setUserId(userDevice.getUserId());
        List<UserDevice> userDeviceList =  userDeviceManager.queryList(userDeviceOption);
        if (userDeviceList== null || userDeviceList.isEmpty()){
            UserDevice addData =  userDeviceManager.addUserDevice(userDevice);
            if ( addData == null ){
                LOGGER.info("addUserDevice:userId:[{}],deviceId:[{}]绑定异常", userDevice.getUserId(), userDevice.getDeviceId());
                return ImmutableTriple.of(Boolean.FALSE, "绑定异常", null);
            }
            return ImmutableTriple.of(Boolean.TRUE, null,  addData);
        }
        UserDevice queryData = userDeviceList.get(0);
        if ( queryData.getStatus() ){
            LOGGER.info("addUserDevice:userId:[{}],deviceId:[{}]已经绑定", userDevice.getUserId(), userDevice.getDeviceId());
            return ImmutableTriple.of(Boolean.FALSE, "已经绑定", null);
        }

        LOGGER.info("addUserDevice:userId:[{}],deviceId:[{}] 已经有数据了，但是已经解绑了", userDevice.getUserId(), userDevice.getDeviceId());

        UserDevice updateData = new UserDevice();
        updateData.setId(queryData.getId());
        updateData.setStatus(Boolean.TRUE);
        updateData.setUserId(userDevice.getUserId());

        UserDevice updatedData =  userDeviceManager.updateUserDevice(updateData);
        if (updatedData == null ){
            LOGGER.info("addUserDevice:userId:[{}],deviceId:[{}] 绑定异常", userDevice.getUserId(), userDevice.getDeviceId());
            return ImmutableTriple.of(Boolean.FALSE, "绑定异常", null);
        }
        return ImmutableTriple.of(Boolean.TRUE, null, updatedData);
    }

    @Override
    public ImmutableTriple<Boolean, String, UserDevice> updateUserDevice(UserDevice userDevice) {
        UserDevice updateData =  userDeviceManager.updateUserDevice(userDevice);
        if ( updateData == null ){
            return ImmutableTriple.of(Boolean.FALSE, "修改异常", null);
        }
        return ImmutableTriple.of(Boolean.TRUE, null,  updateData);
    }

    @Override
    public List<UserDevice> queryListByUserId(String userId, UserDeviceOption userDeviceOption) {
        if ( userDeviceOption == null ){
            userDeviceOption = new UserDeviceOption();
        }
        userDeviceOption.setUserId(userId);
        return  userDeviceManager.queryList(userDeviceOption);
    }

    @Override
    public List<UserDevice> queryListByDeviceId(String deviceId, UserDeviceOption userDeviceOption) {
        if ( userDeviceOption == null ){
            userDeviceOption = new UserDeviceOption();
        }
        userDeviceOption.setDeviceId(deviceId);
        return  userDeviceManager.queryList(userDeviceOption);
    }

    @Override
    public ImmutablePair<Boolean, String> unbind(Integer id, String userId) {
        UserDevice userDevice = userDeviceManager.queryById(id);
        if ( userDevice == null ){
            return ImmutablePair.of(Boolean.FALSE, "查询不到数据");
        }

        if ( !userDevice.getStatus() ){
            return ImmutablePair.of(Boolean.FALSE, "已经解绑了");
        }
        userDevice.setStatus(Boolean.FALSE);

        UserDevice updateUserDevice = new UserDevice();
        updateUserDevice.setId(id);
        updateUserDevice.setStatus(Boolean.FALSE);
        updateUserDevice.setUpdatedBy(userId);
        userDevice = userDeviceManager.updateUserDevice(updateUserDevice);
        if ( userDevice == null ){
            return ImmutablePair.of(Boolean.FALSE, "解绑失败了");
        }
        return ImmutablePair.of(Boolean.TRUE, null);
    }
}
